<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@taglib prefix="mvc" uri="http://www.springframework.org/tags/form" %>
<%@taglib prefix="fmt" uri="http://www.springframework.org/tags" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE html>
<html>
<head>
    <title>选择设备</title><%--选择设备--%>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=10,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
    <link rel="stylesheet" type="text/css" href="../css/common/style.css" />
    <link rel="stylesheet" type="text/css" href="../css/common/select.css" />
    <link rel="stylesheet" type="text/css" href="../css/common/org_select.css">
    <link rel="stylesheet" type="text/css" href="../lib/easyui/themes/easyui.css"/>
    <link rel="stylesheet" type="text/css" href="../lib/easyui/themes/icon.css"/>
    <script type="text/javascript" src="/lib/jQuery-File-Upload-master/jquery-1.8.3.min.js"></script>
    <script src="/js/base/base.js"></script>
    <script type="text/javascript" src="../lib/easyui/jquery.easyui.min.js" ></script>
    <script type="text/javascript" src="../lib/easyui/tree.js" ></script>
</head>
<style type="text/css">
    #dept_menu{
        overflow-x: auto;
    }
    .left_choose ul li div,.left_choose ul li h1,.left_choose ul img{
        float:left;
    }
    .left_choose ul img{
    <!-- margin-top:8px; -->
    }
    .left_choose ul li{
        width:80%;
        height:20px;
    <!-- background:red; -->
        margin-top:10px;
    }
    .mar{
        margin-left:10%;
    }
    .name li{
        list-style-type:none;
    }
    .choose{
        background:#D6E4EF !important;
    }
    #block-right-header,#block-right-add,#block-right-remove{
        display: none;
    }
    .onlineRight{
        width: 98%;
        left: 0 !important;
        border-left: none !important;
        padding: 8px !important;
        margin: 0 auto;
    }
    #allPriv .block-left-item:hover{
        background: #e7f2fa;
    }
    .privActive{
        background: #c7e1f6 !important;
        font-weight: bold;
    }
    .textLeft{
        text-align: left !important;

    }
    #allGroup .block-left-item:hover{
        background: #e7f2fa;
    }
    #query_userId{
        border: 1px solid #d9d9d9;
        width: 91%;
        height: 100px;
        border-radius: 4px;
        margin-top: 15px;
        margin-left: 20px;
        color: #666;
        padding: 5px;
    }

    .addcustom{
        position: fixed;
        bottom: 5px;
        /* display: block; */
        color: #fff;
        left: 32px;
        background-color: #5bc0de;
        border-color: #46b8da;
        padding: 6px 12px;
        width: 106px;
        text-align: center;
        border-radius: 2px;
        z-index: 99999;
        cursor: pointer;
    }
    /*.tree-node .tree-hit{*/
        /*display: none;*/
    /*}*/
</style>
<body>
<!-- //开始 -->
<!-- //头部 -->

<div id="north">
    <div id="navMenu" style="width:auto;">
        <a href="#" title='<fmt:message code="common.th.selPerson" />' class="tab_btn"  block="selected" hidefocus="hidefocus"><span>已选设备</span></a><%--已选人员 已选--%>
        <a href="#" title='<fmt:message code="common.th.selByDepart" />' block="dept" hidefocus="hidefocus" class=" tab_btn active"><span>设备</span></a><%--按部门选择 部门--%>
        <%--<a href="#" title="按角色选择" id="privBtn" class="tab_btn" block="priv" hidefocus="hidefocus"><span>角色</span></a>--%>
        <%--<a href="#" title="按分组选择" id="groupBtn" class="tab_btn" block="group" hidefocus="hidefocus"><span>公共分组</span></a>--%>
        <%--<a href="#" title="按自定义组选择" id="customGroup" class="tab_btn" block="custom" hidefocus="hidefocus"><span>个人分组</span></a>--%>
        <%--<a href="#" title="从在线人员选择" id="userOnline" class="tab_btn" block="online" hidefocus="hidefocus"><span>在线</span></a>--%>
        <a href="#" block="query" class="tab_btn" hidefocus="hidefocus" style="display:none;"><span><fmt:message code="workflow.th.sousuo" /></span></a><%--搜索--%>
    </div>
    <div id="navRight" style="float:right;">
        <div class="search">
            <div class="search-body">
                <div class="search-input"><input notlogin_flag="" id="keyword" type="text" value=""></div>
                <div id="search_clear" class="search-clear" style="display:none;"></div>
            </div>
        </div>
    </div>
</div>

<!-- //内容 -->
<div>
    <!-- 部门 -->
    <div class="main-block" id="deptBox" style="display:block;">
        <div class="left moduleContainer" id="dept_menu">

            <div class="tree">
                <div class="pickCompany">
                            <span  class="childdept" style="display: none;">
                                <img src="/img/commonTheme/${sessionScope.InterfaceModel}/icon_company.png" alt="" style="vertical-align: middle;width: 15px;margin-right: 5px;margin-left: 5px;margin-bottom: 4px;">
                                <%--<input type="checkbox" id="checkedUserAll" value="" style="width: 15px;height: 15px;">--%>
                                <a href="javascript:void(0)" class="dynatree-title" onclick="initTree();"  id="companyName" title=""></a>
                            </span>
                </div>
                <ul class="dynatree-container dynatree-no-connector" style="margin-left: 10px;" id="deptOrg">
                </ul>
            </div>
        </div>
        <div class="right" id="dept_item">
            <div class="block-right" id="dept_item_2">
                <!-- 部门名 -->
                <div class="block-right-header" title="" style="background: #b6e0ff;" id="block-right-header"></div>

                <div id="block-right-add" onclick="addAllUser($('#deptBox'))" class="block-right-add"><fmt:message code="meet.th.addAll" /></div><%--全部添加--%>
                <div id="block-right-remove" onclick="deleteAllUser($('#deptBox'))" class="block-right-remove"><fmt:message code="meet.th.DeleteAll" /></div><%--全部删除--%>
                <div class="userItem curDept">

                </div>
            </div>
        </div>
    </div>
    <!-- 已选 -->
    <div class="main-block" id="selectedDox" >
        <div class="left moduleContainer" id="dept_menu">
            <div class="tree">
                <ul class="dynatree-container dynatree-no-connector" id="deptOrg">
                </ul>
            </div>

        </div>
        <div class="right" id="dept_item">
            <div class="block-right" id="dept_item_2">
                <!-- 部门名 -->

                <div id="block-right-remove" class="block-right-remove"><fmt:message code="meet.th.DeleteAll" /></div><%--全部删除--%>

                <div class="userItem">
                </div>
            </div>
        </div>
    </div>
    <!-- 搜索 -->
    <div class="main-block" id="searchName" style="display: none;">
        <div class="right onlineRight" id="search_online">
            <div class="block-right" id="search_onlineUser" style="display: block;">
                <div class="block-right-header" title="" style="background: #b6e0ff;" id="onlineUser">查询人员</div>
                <div id="onlineAdd" class="block-right-add" onclick="addAllUser($('#searchName'))"><fmt:message code="meet.th.addAll" /></div><%--全部添加--%>
                <div class="block-right-remove" onclick="deleteAllUser($('#searchName'))"><fmt:message code="meet.th.DeleteAll" /></div><%--全部删除--%>

                <div class="userItem">

                </div>
            </div>
            <div class="noUsers" style="display: none;text-align: center;line-height: 100px;font-size: 12pt;">无符合条件的用户</div>
        </div>
    </div>
</div>
<!-- //结束 -->
<div id="south">
    <%--<span class="chooseBox">已选人员：</span><span class="chooseBox" style="color: red;margin-right: 10px;" data-num="0">0人</span>--%>
    <input type="button" class="BigButtonA" value='<fmt:message code="global.lang.ok" />' onclick="close_window();">&nbsp;&nbsp;<%--确定--%>
</div>
</body>
<script type="text/javascript">
    var classDept ;
    if(parent.classDept ){
        classDept = parent.opener.classDept;
    }
    paraValue=""
    searchPara=""
    $.ajax({
        type:'get',
        url:'/syspara/queryOrgScope',
        dataType:'json',
        success:function (reg) {
            var item=reg.object;
            if(item.paraValue == 1){
                paraValue = 1;
            }
        }
    })

    var urlType=location.href.split('?')[1];
    var onLine=[];
    function throttle(method,text) {
        clearTimeout(method.tId);
        method.tId=setTimeout(function(){
            method.call(method,text);
        },500);
    }
    var initTree;
    var user_id='';
    var userPrivName='';
    var dataid = '';
    var selectItemsName ='';
    var parentObj='';
    if(parent.opener && parent.opener.user_id){
        user_id = parent.opener.document.getElementById(parent.opener.user_id).getAttribute('user_id');
        userPrivName = parent.opener.document.getElementById(parent.opener.user_id).getAttribute('userPrivName');
        dataid = parent.opener.document.getElementById(parent.opener.user_id).getAttribute('dataid');
        selectItemsName = parent.opener.document.getElementById(parent.opener.user_id).value;
    }else{
        user_id = parent.$("#"+parent.user_id).attr('user_id');
        userPrivName = parent.$("#"+parent.user_id).attr('userPrivName');
        dataid = parent.$("#"+parent.user_id).attr('dataid');
        selectItemsName = parent.$("#"+parent.user_id).val();
        parentObj = parent.$("#"+parent.user_id);

    }
    console.log(user_id)


    function autodivheights(text) {
        var url = "/crash/showAssets?assetName=" + text
        $.ajax({
            type: "get",
            url: url,
            dataType: 'JSON',
            success: function (res) {
                    if (res.data.length > 0) {  //判断是否返回数据集合
                        var arr = [];
                        $('#search_onlineUser').show();
                        $('#searchName').find('.noUsers').hide()
                        res.data.forEach(function (v, i) {
                            if (v.id) {
                                var divObj = {};
                                divObj = $('<div class="block-right-item ' + function () {
                                        if (user_id) {
                                            var user_idArr;
                                            if (Array.isArray(user_id)) {
                                                user_idArr = user_id;
                                            } else {
                                                user_idArr = user_id.split(',');
                                            }

                                            var bool = false;
                                            for (var i = 0; i < user_idArr.length; i++) {
                                                if (v.userId == user_idArr[i]) {
                                                    bool = true;
                                                    break;
                                                }
                                            }
                                            if (bool) {
                                                return 'active';
                                            } else {
                                                return ''
                                            }
                                        } else {
                                            return ''
                                        }
                                    }() + '" user_id="' + v.id + '"  assetsname="' + function () {
                                        if (v.assetsName == undefined) {
                                            return ''
                                        } else {
                                            return v.assetsName
                                        }
                                    }() + '"  title="' + v.assetsName + '"><span class="name">' + v.assetsName + '</span></div>');

                                if ($('#selectedDox .userItem #' + v.id).length > 0) {
                                    divObj.addClass('active')
                                }
                                arr.push(divObj)
                            }
                        });
                        $('#searchName .userItem').html(arr);
                    } else {
                        $('#search_onlineUser').hide();
                        $('#searchName').find('.noUsers').show()
                    }
            }
        });
    }

    function TextareaBlurFun(e){
        if(e.val() == '我是...'){
            e.val('我是');
        }
    }

    function close_window(){
        var itemsArr = $('#selectedDox .userItem .active');
        var itemnum=location.href.split('?')[1]
        if(itemnum==0){
            if(itemsArr.length>1){
                layer.msg('<fmt:message code="common.th.onlyChooseOne" />', {icon: 5});
                //alert('<fmt:message code="common.th.onlyChooseOne" />')/*只能选择一个*/
                return
            }

        }
        var selectItemsId = '';
        var selectItemsName = '';
        var selectUid = '';
        var userPrivName = ''
        for(var i=0;i<itemsArr.length;i++){
            var obj = itemsArr.eq(i);
            selectItemsId+=(obj.attr("user_id")+',');
            selectItemsName+=(obj.attr("assetsname")+',');
            selectUid+=(obj.attr("user_id")+',');
            userPrivName+=(obj.attr("assetsname")+',');
        };
        if(parent.opener && parent.opener.user_id){

            parent.opener.document.getElementById(parent.opener.user_id).value=selectItemsName;
            parent.opener.document.getElementById(parent.opener.user_id).setAttribute('username',selectItemsName);
            parent.opener.document.getElementById(parent.opener.user_id).setAttribute('dataid',selectUid);
            parent.opener.document.getElementById(parent.opener.user_id).setAttribute('user_id',selectItemsId);
            parent.opener.document.getElementById(parent.opener.user_id).setAttribute('userPrivName',userPrivName);
        }else{
            parentObj.val(selectItemsName);
            parentObj.attr('username',selectItemsName);
            parentObj.attr('dataid',selectUid);
            parentObj.attr('user_id',selectItemsId);
            parentObj.attr('userPrivName',userPrivName);
        }
        if(parent.opener){
            if(parent.opener.loadfile!=undefined){
                parent.opener.loadfile(parent.opener.user_id)
            }
        }

        window.close();
        parent.layer.closeAll();
    }

    $(function() {
        //            点击选择全部人员复选框
        var thisUserName = '';
        $.ajax({
            type: 'post',
            url: '/imfriends/getUserName',
            dataType: 'json',
            success: function (data) {
                thisUserName = data.object;
                $('#south').attr('username', data.object)
            }
        })
        $('#checkedUserAll').click(function () {
            var state = $(this).prop('checked');
            var deptName = $('#companyName').text()
            showUserAndDept(0, deptName, function () {
                if (state == true) {
                    $('#deptBox').find('.block-right-item').addClass('active');
                    var str = '';
                    $('#deptBox .userItem .active').each(function (i, v) {
                        if ($(v).attr('user_id') != '' || $(v).attr('user_id') != undefined) { //判断user_id是否为空
                            if ($('#selectedDox .userItem #' + $(v).attr('user_id')).length < 1) {
                                str += $(this).prop("outerHTML");
                            }
                        }
                    });
                    $('#selectedDox .userItem').append(str);
                } else {
                    $('#deptBox').find('.block-right-item').removeClass('active');
                    $('#selectedDox .userItem .block-right-item').each(function () {
                        if ($('#deptBox .userItem #' + $(this).attr('user_id')).length > 0) {
                            $('#deptBox .userItem #' + $(this).attr('user_id')).removeClass('active');
                        }
                    });
                    $('#selectedDox .userItem .block-right-item').remove();
                }
            })
        })
        $.ajax({
            type: 'get',
            url: '/user/getOnlineMap',
            dataType: 'json',
            async: false,
            success: function (res) {
                var data = res.object;
                if (res.flag) {
                    for (var key in data) {
                        var arrKey = [];
                        arrKey.push(key);
                        onLine.push(arrKey)
                    }
                }
            }
        })
        var numIndex = 0;
        buildDefaultItem();
        function buildDefaultItem(){
            var arr = ''
            if(user_id){
                user_id = user_id.split(',');
                if(dataid!=undefined){
                    var userdataid =dataid.split(',');
                }else{
                    var userdataid ='';
                }
                if(userPrivName!=undefined){
                    var thisuserPrivName =userPrivName.split(',');
                }else{
                    var thisuserPrivName ='';
                }
                selectItemsName = selectItemsName.split(',');
                for(var i=0;i<user_id.length;i++){
                    if(selectItemsName[i] && user_id[i]) {
//                            arr += '<div class="block-right-item active"  userprivname="'+function(){if(userPrivName[i] == undefined){return ''}else{return userPrivName[i]}}()+'"  item_id="' + dataid[i] + '" item_name="' + selectItemsName[i] + '"  id="' + dataid[i] + '"  user_id="' + user_id[i] + '" uid="' + dataid[i] + '" title="' + selectItemsName[i] + '"><span class="name">' + selectItemsName[i] + ' ' + function(){if(userPrivName[i] == undefined){return ''}else{return userPrivName[i]}}() + '<span class="status"></span></span></div>';
                        arr += '<div class="block-right-item active" assetsname="' +selectItemsName[i]+ '" userprivname="' + thisuserPrivName[i] + '" uid="' + userdataid[i] + '" id="' + user_id[i] + '"  user_id="' + user_id[i] + '"><span class="name">' + selectItemsName[i] + '<span class="status"></span></span></div>';
                    }
                }
                $('#selectedDox .userItem').append(arr);
            }else if(dataid){
                dataid =dataid.split(',');
                selectItemsName = selectItemsName.split(',');
                for(var i=0;i<dataid.length;i++){
                    if(selectItemsName[i] && dataid[i]) {
//                            arr += '<div class="block-right-item active"  userprivname="'+function(){if(userPrivName[i] == undefined){return ''}else{return userPrivName[i]}}()+'"  item_id="' + dataid[i] + '" item_name="' + selectItemsName[i] + '"  id="' + dataid[i] + '"  user_id="' + user_id[i] + '" uid="' + dataid[i] + '" title="' + selectItemsName[i] + '"><span class="name">' + selectItemsName[i] + ' ' + function(){if(userPrivName[i] == undefined){return ''}else{return userPrivName[i]}}() + '<span class="status"></span></span></div>';
                        arr += '<div class="block-right-item active" assetsname="' +selectItemsName[i]+ '"  uid="' + dataid[i] + '" id="' + dataid[i] + '"><span class="name">' + selectItemsName[i] + '<span class="status"></span></span></div>';
                    }
                }
                $('#selectedDox .userItem').append(arr);
            }
            var num = $('#selectedDox .userItem .block-right-item').length;
            $('.chooseBox').eq(1).html(num+'人').attr('data-num',num);
        };
        initTree = function () {
            var url = "/cpAssetsType/showYjType";
            $('#deptOrg').tree({
                url: url,
                animate: true,
//                checkbox: true,
                loadFilter: function (node) {
                    numIndex++;

                    if (node.msg == '查询成功') {
                        if (node.data.length > 0) {
                            for (var i = 0; i < node.data.length; i++) {
                                searchPara += node.data[i].typeId + ','
                            }
                        }

                    }
                    return convert(node.data);
                },
                onClick: function (node) {
                    buildNewUserList(node.id, node.text);
                },
                onBeforeExpand: function (node, param) {
//                    console.log(node)
                    $('#deptOrg').tree('options').url = "/cpAssetsType/showChild?parentId=" + node.id;// change the url
                },
                onCheck: function (node, checked) {
                    showUserAndDept(node.id, node.text, function () {
                        if (checked) {
                            $('#deptBox').find('.block-right-item').addClass('active');
                            var str = '';
                            $('#deptBox .userItem .active').each(function (i, v) {
                                if ($(v).attr('user_id') != '' || $(v).attr('user_id') != undefined) { //判断user_id是否为空
                                    if ($('#selectedDox .userItem #' + $(v).attr('user_id')).length < 1) {
                                        str += $(this).prop("outerHTML");
                                    }
                                }
                            });
                            $('#selectedDox .userItem').append(str);
                        } else {
                            $('#deptBox').find('.block-right-item').removeClass('active');
                            $('#selectedDox .userItem .block-right-item').each(function () {
                                if ($('#deptBox .userItem #' + $(this).attr('user_id')).length > 0) {
                                    $('#deptBox .userItem #' + $(this).attr('user_id')).removeClass('active');
                                }
                            });
                            $('#selectedDox .userItem .block-right-item').remove();
                        }
                    });
                }

            });
        }
        $.ajax({
            url: '../sys/showUnitManage',
            type: 'get',
            dataType: 'json',
            success: function (obj) {
                $('#companyName').html(obj.object.unitName);
                $('.childdept').show();
                initTree();
            }
        });
        //搜索
        $('#keyword').bind('input propertychange', function () {
            var text = encodeURI($(this).val());
            $('#searchName').show();
            if (text != '') {
                throttle(autodivheights, text);
            } else {
                $('#searchName .userItem').html('');
            }
        });
        $('#selectedDox #block-right-remove').on('click', function () {
            $('#selectedDox .userItem .block-right-item').each(function () {
                if ($('#deptBox .userItem #' + $(this).attr('user_id')).length > 0) {
                    $('#deptBox .userItem #' + $(this).attr('user_id')).removeClass('active');
                }
            });
            $('#selectedDox .userItem .block-right-item').remove();
        });

        $('#selectedDox .userItem ').on('click', '.block-right-item', function () {
            $('#deptBox .userItem #' + $(this).attr('user_id')).removeClass('active');
            $(this).remove();
        })

        //			点击部门显示本部门及子部门人员
        function buildNewUserList(id, text) {
            $('#block-right-header').text(text)
            $.ajax({
                type: 'get',
                url: '/crash/showAssets?typeId=' + id,
                dataType: 'json',
                success: function (res) {
                    var tr = [];
                    var num = 0;
                    var length = $('#selectedDox #dept_item .block-right-item').length;
                    var chooseuidarr = [];
                    var choosearr = [];
                    for (var i = 0; i < length; i++) {
                        var uid = $('#selectedDox #dept_item .block-right-item').eq(i).attr('uid');
                        var userid = $('#selectedDox #dept_item .block-right-item').eq(i).attr('user_id');
                        chooseuidarr.push(uid);
                        choosearr.push(userid);
                    }
                    res.data.forEach(function (v, i) {
                        var divObj;
                        if (v.id) {
                            $('#block-right-header').show();
                            $('#block-right-add').show();
                            $('#block-right-remove').show();
                            num++;
                            divObj = $('<div class="block-right-item ' + function () {
                                    if (choosearr || chooseuidarr) {
                                        var user_idArr;
                                        var uidArr;
                                        if (Array.isArray(choosearr)) {
                                            user_idArr = choosearr;
                                        } else {
                                            user_idArr = choosearr.split(',');
                                        }
                                        if (chooseuidarr != null) {
                                            if (Array.isArray(chooseuidarr)) {
                                                uidArr = chooseuidarr;
                                            } else {
                                                uidArr = chooseuidarr.split(',');
                                            }
                                        }
                                        var bool = false;
                                        if (chooseuidarr != null) {

                                            for (var i = 0; i < uidArr.length; i++) {
                                                if (v.id == uidArr[i]) {
                                                    bool = true;
                                                    break;
                                                }
                                            }
                                        } else if (choosearr) {
                                            for (var i = 0; i < user_idArr.length; i++) {
                                                if (v.id == user_idArr[i]) {
                                                    bool = true;
                                                    break;
                                                }
                                            }
                                        }
                                        if (bool) {
                                            return 'active';
                                        } else {
                                            return ''
                                        }
                                    } else {
                                        return ''
                                    }
                                }() + '" user_id="' + v.id + '"   assetsName="' + function () {
                                    if (v.assetsName == undefined) {
                                        return ''
                                    } else {
                                        return v.assetsName
                                    }
                                }() + '" title="' + v.assetsName + '">' +
                                '<span class="name">' + v.assetsName + '</span></div>');
                            tr.push(divObj);
                        }

                    });
                    if (num == 0) {
                        $('#block-right-header').hide();
                        $('#block-right-add').hide();
                        $('#block-right-remove').hide();
                        tr.push($('<div style="width: 230px;height: 200px;margin: 20px auto;text-align: center">' +
                            '<img src="/img/common/dianjikong.png" style="margin-top: 20px;" alt="">\
                            <span style="width: 100%;display: inline-block;margin-top: 10px;\
                        font-size: 15px;\
                        font-weight: bold;">本类别暂无设备</span>' +
                            '</div>'))
                    } else {
                        $('#block-right-add').show();
                        $('#block-right-remove').show();
                    }
                    $('#deptBox .userItem').html(tr);

                }
            })
        }

        //            点击左侧树中部门前的复选框，显示该部门人员及该部门下的子部门和人员
        function showUserAndDept(id, text, fn) {
            $('#block-right-header').text(text);
            $.ajax({
                type: 'get',
                url: '/crash/showAssets?typeId=' + id,
                dataType: 'json',
                success: function (res) {
                    var data = res.data;
                    var str = '';
                    var objStr = '';
                        if (data.length > 0) {
                            for (var i = 0; i < data.length; i++) {
                                objStr += '<div class="block-right-item ' + function () {
                                        if (user_id) {  //判断user_id是否存在
                                            var user_idArr;
                                            if (Array.isArray(user_id)) {
                                                user_idArr = user_id;
                                            } else {
                                                user_idArr = user_id.split(',');
                                            }
                                            var bool = false;
                                            for (var j = 0; j < user_idArr.length; j++) {
                                                if (data[i].id == user_idArr[j]) {
                                                    bool = true;
                                                    break;
                                                }
                                            }
                                            if (bool) {
                                                return 'active';
                                            } else {
                                                return ''
                                            }
                                        } else {
                                            return ''
                                        }
                                    }() + '" userPrivName="' + function () {
                                        if (data[i].userPrivName == undefined) {
                                            return ''
                                        } else {
                                            return data[i].userPrivName
                                        }
                                    }() + '" user_id="' + data[i].id + '" title="' + data[i].assetsName + '"><span class="name">' + data[i].assetsName + '</div>';
                            }
                        }
                        $('#deptBox').find('.userItem').html(objStr + str)

                        if (fn) {
                            fn();
                        }
                    }
            })
        }

        function TreeNode(id, text, state, children) {
            this.id = id;
            this.text = text;
            this.state = state;
            this.children = children;
        }

        function convert(data) {
            var arr = [];
            var tr = '';
            var defTr = ''
            data.forEach(function (v, i) {
                if (v.typeId) {
                    var node = {};
                    if (numIndex == '1') {
                        node = new TreeNode(v.typeId, v.typeName, "closed")
                    } else {
                        node = new TreeNode(v.typeId, v.typeName, "closed")
                    }
                    arr.push(node);
                }
            });
            return arr;
        }
        //组织
        $('.tab_btn').click(function () {
            var type = $(this).attr('block');
            $(this).addClass("active").siblings().removeClass('active');
            switch (type) {
                case "selected":
                    $('#selectedDox').show().siblings().hide();
                    break;
                case "dept":
                    $('#deptBox').show().siblings().hide();
                    if ($('.tree-node-selected').length != 0) {
                        $('.tree-node-selected').click();
                    } else {
                        var itemsArr = $('#selectedDox .userItem .active');
                        var selectItemsId = '';
                        for (var i = 0; i < itemsArr.length; i++) {
                            if (i != (itemsArr.length - 1)) {
                                var obj = itemsArr.eq(i);
                                selectItemsId += (obj.attr("user_id") + ',');
                            } else {
                                var obj = itemsArr.eq(i);
                                selectItemsId += obj.attr("user_id");
                            }
                        }
                        user_id = selectItemsId;
                    }
                    break;
                case "query":

                    break;
            }
        });

        $('.tree .dynatree-container').on('click', '.childdept', function () {
            var that = $(this);
            getChDept(that.next(), that.attr('deptid'));
            var title = that.find('a').text();
            $('.block-right-header').html(title);
        });
        $('.right').on("click", ".block-right-item", function () {
            var that = $(this);
            if (that.attr('class').indexOf('active') > 0) {
                that.removeClass("active");
                if ($('#selectedDox .userItem #' + that.attr('user_id')).length > 0) {

                    $('#selectedDox .userItem #' + that.attr('user_id')).remove();
                }
            } else {
                var divObj = $(that.prop("outerHTML"));
                divObj.addClass("active");
                that.addClass("active");
                if ($('#selectedDox .userItem #' + that.attr('user_id')).length < 1) {
                    $('#selectedDox .userItem').append(divObj);
                }
                if (urlType == 0) {
                    that.siblings('div').removeClass('active')
                    $('#selectedDox .userItem').empty()
                    $('#selectedDox .userItem').append(divObj);
                }
            }
        });
    });

    //			点击全部添加方法
    function addAllUser(element) {
        element.find('.userItem .block-right-item').addClass('active');
        var str  = '';
        element.find('.userItem .active').each(function (i,v) {
            if( $('#selectedDox .userItem #'+$(this).attr('user_id')).length < 1){
                str += $(this).prop("outerHTML");
            }
        });
        $('#selectedDox .userItem').append(str);
    }
    //        点击全部删除方法
    function deleteAllUser(element) {
        element.find('.userItem .active').each(function (i, v) {
            if ($('#selectedDox .userItem #' + $(this).attr('user_id')).length > 0) {
                $('#selectedDox .userItem #' + $(this).attr('user_id')).remove();
            }
        });
        element.find('.userItem .block-right-item').removeClass('active')
    }

</script>
</html>
